﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.ComponentModel;

using Atosenet.PluginEngine;


namespace Atosenet.IO
{
    [PluginAttribute("Atosenet", "Atosenet.IO.UdpClientIOParameter", "UDP客户端配置", PluginType.IOParameter)]
    public class UdpClientIOParameter : IOParameter
    {
        #region 成员

        public new const string CATEGORY_NAME = "UDP客户端设置";
        public static UdpClientIOParameter Default = new UdpClientIOParameter("127.0.0.1", 2012);

        private string _remoteIp = string.Empty;
        private int _remotePort = 0;
        private int _localPort = 0;        

        #endregion


        #region 初始化

        public UdpClientIOParameter()
            : this("127.0.0.1", 2012)
        { }

        public UdpClientIOParameter(string remoteIp, int remotePort)
        {
            _remoteIp = remoteIp;
            _remotePort = remotePort;
        }

        public UdpClientIOParameter(string remoteIp, int remotePort, int localPort)
        {
            _remoteIp = remoteIp;
            _remotePort = remotePort;
            _localPort = localPort;
        }

        #endregion


        #region 属性

        [CategoryAttribute(CATEGORY_NAME)]
        [DescriptionAttribute("远程连接IP地址")]
        public string RemoteIP
        {
            get { return _remoteIp; }
            set
            {
                if (_remoteIp != value)
                {
                    _remoteIp = value;
                    OnParameterChanged();
                }
            }
        }

        [CategoryAttribute(CATEGORY_NAME)]
        [DescriptionAttribute("远程连接端口")]
        public int RemotePort
        {
            get { return _remotePort; }
            set
            {
                if (_remotePort != value)
                {
                    _remotePort = value;
                    OnParameterChanged();
                }
            }
        }

        [CategoryAttribute(CATEGORY_NAME)]
        [DescriptionAttribute("本地端口")]
        public int LocalPort
        {
            get { return _localPort; }
            set
            {
                if (_localPort != value)
                {
                    _localPort = value;
                    OnParameterChanged();
                }
            }
        }

        [CategoryAttribute(CATEGORY_NAME)]
        [DescriptionAttribute("是否为随机端口")]
        public bool IsRandomPort
        {
            get { return _localPort == 0; }
            set
            {
                _localPort = value ? 0 : RandomGenerator.Next(1025, 5000);
            }
        }

        [DescriptionAttribute("IO名称")]
        public override string IOName
        {
            get { return "UDP客户端连接"; }
        }

        #endregion
    }
}
